package com.scheduling.machine.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scheduling.common.result.Result;
import com.scheduling.machine.entity.MachineTypeMaterialCompatibility;
import com.scheduling.machine.service.IMachineTypeMaterialCompatibilityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 机器类型原料兼容性管理控制器
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/api/machine-type-material-compatibility")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "机器类型原料兼容性管理", description = "机器类型与原料兼容性配置相关接口")
public class MachineTypeMaterialCompatibilityController {

    private final IMachineTypeMaterialCompatibilityService compatibilityService;

    @PostMapping
    @Operation(summary = "创建兼容性配置", description = "创建机器类型与原料的兼容性配置")
    public Result<MachineTypeMaterialCompatibility> createCompatibility(
            @Valid @RequestBody MachineTypeMaterialCompatibility compatibility) {
        try {
            MachineTypeMaterialCompatibility result = compatibilityService.createCompatibility(compatibility);
            return Result.success(result, "兼容性配置创建成功");
        } catch (Exception e) {
            log.error("创建兼容性配置失败", e);
            return Result.error("创建兼容性配置失败: " + e.getMessage());
        }
    }

    @PutMapping("/{compatibilityId}")
    @Operation(summary = "更新兼容性配置", description = "更新机器类型与原料的兼容性配置")
    public Result<MachineTypeMaterialCompatibility> updateCompatibility(
            @Parameter(description = "兼容性配置ID") @PathVariable String compatibilityId,
            @Valid @RequestBody MachineTypeMaterialCompatibility compatibility) {
        try {
            compatibility.setCompatibilityId(compatibilityId);
            MachineTypeMaterialCompatibility result = compatibilityService.updateCompatibility(compatibility);
            return Result.success(result, "兼容性配置更新成功");
        } catch (Exception e) {
            log.error("更新兼容性配置失败: {}", compatibilityId, e);
            return Result.error("更新兼容性配置失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{compatibilityId}")
    @Operation(summary = "删除兼容性配置", description = "删除机器类型与原料的兼容性配置")
    public Result<Void> deleteCompatibility(
            @Parameter(description = "兼容性配置ID") @PathVariable String compatibilityId) {
        try {
            boolean success = compatibilityService.deleteCompatibility(compatibilityId);
            if (success) {
                return Result.success(null, "兼容性配置删除成功");
            } else {
                return Result.error("兼容性配置删除失败");
            }
        } catch (Exception e) {
            log.error("删除兼容性配置失败: {}", compatibilityId, e);
            return Result.error("删除兼容性配置失败: " + e.getMessage());
        }
    }

    @GetMapping("/{compatibilityId}")
    @Operation(summary = "查询兼容性配置详情", description = "根据ID查询兼容性配置详情")
    public Result<MachineTypeMaterialCompatibility> getCompatibilityDetail(
            @Parameter(description = "兼容性配置ID") @PathVariable String compatibilityId) {
        try {
            MachineTypeMaterialCompatibility result = compatibilityService.getCompatibilityDetail(compatibilityId);
            return Result.success(result, "查询成功");
        } catch (Exception e) {
            log.error("查询兼容性配置详情失败: {}", compatibilityId, e);
            return Result.error("查询兼容性配置详情失败: " + e.getMessage());
        }
    }

    @GetMapping
    @Operation(summary = "分页查询兼容性配置", description = "分页查询机器类型与原料兼容性配置列表")
    public Result<Page<MachineTypeMaterialCompatibility>> getCompatibilityPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") long size,
            @Parameter(description = "机器类型ID") @RequestParam(required = false) String machineTypeId,
            @Parameter(description = "原料ID") @RequestParam(required = false) String materialId,
            @Parameter(description = "是否兼容") @RequestParam(required = false) Boolean compatible) {
        try {
            Page<MachineTypeMaterialCompatibility> result = compatibilityService.getCompatibilityPage(
                    current, size, machineTypeId, materialId, compatible);
            return Result.success(result, "查询成功");
        } catch (Exception e) {
            log.error("分页查询兼容性配置失败", e);
            return Result.error("查询兼容性配置失败: " + e.getMessage());
        }
    }

    @GetMapping("/machine-type/{machineTypeId}")
    @Operation(summary = "查询机器类型的兼容原料", description = "查询指定机器类型的所有兼容原料")
    public Result<List<MachineTypeMaterialCompatibility>> getCompatibleMaterials(
            @Parameter(description = "机器类型ID") @PathVariable String machineTypeId) {
        try {
            List<MachineTypeMaterialCompatibility> result = compatibilityService.getCompatibleMaterials(machineTypeId);
            return Result.success(result, "查询成功");
        } catch (Exception e) {
            log.error("查询机器类型兼容原料失败: {}", machineTypeId, e);
            return Result.error("查询机器类型兼容原料失败: " + e.getMessage());
        }
    }

    @GetMapping("/material/{materialId}")
    @Operation(summary = "查询原料的兼容机器类型", description = "查询指定原料的所有兼容机器类型")
    public Result<List<MachineTypeMaterialCompatibility>> getCompatibleMachineTypes(
            @Parameter(description = "原料ID") @PathVariable String materialId) {
        try {
            List<MachineTypeMaterialCompatibility> result = compatibilityService.getCompatibleMachineTypes(materialId);
            return Result.success(result, "查询成功");
        } catch (Exception e) {
            log.error("查询原料兼容机器类型失败: {}", materialId, e);
            return Result.error("查询原料兼容机器类型失败: " + e.getMessage());
        }
    }

    @GetMapping("/check")
    @Operation(summary = "检查兼容性", description = "检查指定机器类型与原料是否兼容")
    public Result<Boolean> checkCompatibility(
            @Parameter(description = "机器类型ID") @RequestParam String machineTypeId,
            @Parameter(description = "原料ID") @RequestParam String materialId) {
        try {
            boolean result = compatibilityService.isCompatible(machineTypeId, materialId);
            return Result.success(result, "检查完成");
        } catch (Exception e) {
            log.error("检查兼容性失败: machineTypeId={}, materialId={}", machineTypeId, materialId, e);
            return Result.error("检查兼容性失败: " + e.getMessage());
        }
    }

    @PostMapping("/batch")
    @Operation(summary = "批量创建兼容性配置", description = "批量创建机器类型与原料的兼容性配置")
    public Result<List<MachineTypeMaterialCompatibility>> batchCreateCompatibility(
            @Valid @RequestBody List<MachineTypeMaterialCompatibility> compatibilities) {
        try {
            List<MachineTypeMaterialCompatibility> result =
                    compatibilityService.batchCreateCompatibility(compatibilities);
            return Result.success(result, "批量创建兼容性配置成功，共创建 " + result.size() + " 条记录");
        } catch (Exception e) {
            log.error("批量创建兼容性配置失败", e);
            return Result.error("批量创建兼容性配置失败: " + e.getMessage());
        }
    }

    @PutMapping("/batch-update")
    @Operation(summary = "批量更新兼容性状态", description = "批量更新多个兼容性配置的状态")
    public Result<Integer> batchUpdateCompatibility(
            @Parameter(description = "兼容性配置ID列表") @RequestBody List<String> compatibilityIds,
            @Parameter(description = "新的兼容性状态") @RequestParam Boolean compatible,
            @Parameter(description = "不兼容原因") @RequestParam(required = false) String reason) {
        try {
            int updatedCount = compatibilityService.batchUpdateCompatibility(compatibilityIds, compatible, reason);
            return Result.success(updatedCount, "批量更新成功，影响 " + updatedCount + " 条记录");
        } catch (Exception e) {
            log.error("批量更新兼容性配置失败", e);
            return Result.error("批量更新兼容性配置失败: " + e.getMessage());
        }
    }
} 